<script type="text/javascript">
	function XemTheoKHXuLy()
	{
		document.xemtheokh.submit(); 
	}
</script>
<?php
	include "../../functions.php";
	session_start();
	$nhatky = new nhatkybs();
	$nhatky->TenSuKien = "Sao kê hợp đồng tín dụng theo khách hàng";
	$nhatky->MaNguoiDung = $_SESSION["MaND"];
	date_default_timezone_set('Asia/Ho_Chi_Minh');
	$nhatky->ThoiGian = date("Y-m-d H:m:s", time());
	$nhatky->Them();
?>
<div id="SaoKeHopDongTinDung" style="color: red">	
	<table style="width: 850px">
		<tr>
			<td width="40%"><b>Quỹ tín dụng Nhân dân MỸ HÒA</b></td>
			<td width="30%"></td>
			<td width="40%"></td>
		</tr>
		<tr>
			<td colspan="3"><b><i>Địa chỉ</i></b>: Tỉnh lộ 943, Tây Khánh 4, Phường Mỹ Hòa, Tp Long Xuyên, An Giang</td>
		</tr>
		<tr>
			<td align="center" colspan="3"><h1>SAO KÊ DANH SÁCH HỢP ĐỒNG TÍN DỤNG</h1></td>
		</tr>
		<tr>
			<td colspan="3" align="center"><i><strong>Ngày thực hiện:</strong></i> <?php echo date("d-m-Y", time());?></td>
		</tr>
	</table>
	<br/>
	<br/>
	<table border="1" style="width: 850px;" >
		<tr>
			<th align="center" rowspan="1">
                STT
			</th>
			<th align="center" rowspan="1">
               	Họ tên - Tài khoản
			</th>
			<th align="center" rowspan="1">
               	Số khế ước
			</th>
			<th align="center" rowspan="1">
               	Số tiền ghi nợ
			</th>
			<th align="center" rowspan="1">
               	Số tiền ghi có
			</th>
			<th align="center" rowspan="1">
               	Ghi chú
			</th>	
		</tr>
		<?php 
			$MaKhachHang = $_POST["MaKhachHang"];
			if (!isset($MaKhachHang))
			{
				$MaKhachHang = $_GET['MaKhachHang'];
			}
			$DieuKienMaKH = "";
			if($MaKhachHang!="" && $MaKhachHang!="0")
			{
				$DieuKienMaKH = "WHERE MaKH = '$MaKhachHang'";
			}
			$conn=mysql_connect("localhost","root","vertrigo") or die("Không thể kết nối!");
			mysql_select_db("webqtd")or die("không tồn tại CSDL\"webqtd\"!");
			mysql_query("SET NAMES 'utf8'"); //Để hiển thị unicode
			$sqlgroupkh = "SELECT MaKH, COUNT(MaKH) AS SoDong FROM taisanthechap $DieuKienMaKH GROUP BY MaKH";
			$rsgroupkh = mysql_query($sqlgroupkh);
			$stt =1;
			while($donggroupkh = mysql_fetch_array($rsgroupkh))
			{
				$SoDong = $donggroupkh["SoDong"];
				$khachhang = new khachhangbs();
				$khachhang->MaKH = $donggroupkh["MaKH"];
				$dskhachhang = $khachhang->LayDanhSach();
				$HoTenTaiKhoan = $dskhachhang[0]["Ho"]." ".$dskhachhang[0]["Ten"]." - ".$donggroupkh["MaKH"];
				echo "<tr>";
					echo "<td align='center' rowspan='{$SoDong}'>{$stt}</td>";
					echo "<td rowspan='{$SoDong}'>{$HoTenTaiKhoan}</td>";
					$tstc = new taisanthechapbs();
					$tstc->MaKH = $donggroupkh["MaKH"];
					$dststc = $tstc->LayDanhSach();
					$sohdtcdautien = $dststc[0]["SoHDTC"];
					
					$hopdongtindung = new hopdongtindungbs();
					$hopdongtindung->SoHDTC = $sohdtcdautien;
					$dshopdongtindung = $hopdongtindung->LayDanhSach();
					$hopdongtindungdautien = $dshopdongtindung[0]["SoKheUoc"];
					
					echo "<td align='center'>{$hopdongtindungdautien}</td>";
					$conn=mysql_connect("localhost","root","vertrigo") or die("Không thể kết nối!");
					mysql_select_db("webqtd")or die("không tồn tại CSDL\"webqtd\"!");
					mysql_query("SET NAMES 'utf8'"); //Để hiển thị unicode
					$sqlsumnoco = "SELECT SUM(SoTienTaiKhoanNo) AS TongSoNo, SUM(SoTienTaiKhoanCo) AS TongSoCo FROM kekhaigiaodich WHERE SoKheUoc = '$hopdongtindungdautien'";
					$rssumnoco = mysql_query($sqlsumnoco);
					$dongsumnoco = mysql_fetch_array($rssumnoco);
					$TongSoNo = $dongsumnoco["TongSoNo"];
					$TongSoCo = $dongsumnoco["TongSoCo"];
					echo "<td align='center'>{$TongSoNo}</td>";
					echo "<td align='center'>{$TongSoCo}</td>";
					echo "<td style='font-size:0.0001px;'>.</td>";
				echo "</tr>";
				if(count($dststc)>1)
				{
					for($j = 1; $j < ($dststc != false ? count($dststc) : 1); $j++)
					{
						echo "<tr>";
							$hopdongtindung = new hopdongtindungbs();
							$hopdongtindung->SoHDTC = $dststc[$j]["SoHDTC"];
							$dshopdongtindung = $hopdongtindung->LayDanhSach();
							$hopdongtindung = $dshopdongtindung[0]["SoKheUoc"];
							
							echo "<td align='center'>{$hopdongtindung}</td>";
							$conn=mysql_connect("localhost","root","vertrigo") or die("Không thể kết nối!");
							mysql_select_db("webqtd")or die("không tồn tại CSDL\"webqtd\"!");
							mysql_query("SET NAMES 'utf8'"); //Để hiển thị unicode
							$sqlsumnoco = "SELECT SUM(SoTienTaiKhoanNo) AS TongSoNo, SUM(SoTienTaiKhoanCo) AS TongSoCo FROM kekhaigiaodich WHERE SoKheUoc = '$hopdongtindung'";
							$rssumnoco = mysql_query($sqlsumnoco);
							$dongsumnoco = mysql_fetch_array($rssumnoco);
							$TongSoNo = $dongsumnoco["TongSoNo"];
							$TongSoCo = $dongsumnoco["TongSoCo"];
							echo "<td align='center'>{$TongSoNo}</td>";
							echo "<td align='center'>{$TongSoCo}</td>";
							echo "<td style='font-size:0.0001px;'>.</td>";
						echo "</tr>";
					}
				}
				$stt++;
			}
		?>
		
	</table>
	<br/>
	<br/><br/><br/>
</div>	
<table style="width: 850px">
	<tr>
		<td align="center">
               <input type="button" id="print_button"  value="" title="In bài này" onclick="InTrang();" /> In sao kê
		</td>
	</tr>
</table>
<br/>
<br/>
<form id="xemtheokh" name ="xemtheokh" method="post" action="index.php?do=saokehopdongtindung">
<table style="width: 950px">
		<tr>
			<td style="padding-top:10px;">
				<select name="MaKhachHang">	
            		<option value="0">-- Chọn khách hàng --</option>	                      	
					<?php
						$khachhang = new khachhangbs();
						$danhsachkhachhang = $khachhang->LayDanhSach();
						for($j = 0; $j < ($danhsachkhachhang != false ? count($danhsachkhachhang) : 0); $j++)
						{	
							$HoTen = $danhsachkhachhang[$j]['Ho'] . " "	. $danhsachkhachhang[$j]['Ten'];	
							echo "<option value='{$danhsachkhachhang[$j]["MaKH"]}'>{$HoTen}</option>";
						}
					?>
				</select>
			&nbsp &nbsp	<a href="javascript:XemTheoKHXuLy();" class="button small blue">Xem</a>	
			</td>
		</tr>
</table>
</form>
<br/>
<br/>
<a href="index.php?do=hopdongtindung">Quay về danh sách hợp đồng tín dụng</a>
<script type="text/javascript">
function InTrang()
{ 
	  var disp_setting="toolbar=yes,location=no,directories=yes,menubar=yes,"; 
	      disp_setting+="scrollbars=yes,width=650, height=600, left=100, top=25"; 
	  var content_vlue = document.getElementById("SaoKeHopDongTinDung").innerHTML; 
	  
	  var docprint=window.open("","",disp_setting); 
	   docprint.document.open(); 
	   docprint.document.write('<html><head><title>Quỹ tín dụng Mỹ Hòa</title>'); 
	   docprint.document.write('</head><body onLoad="self.print()"><center>');          
	   docprint.document.write(content_vlue);          
	   docprint.document.write('</center></body></html>'); 
	   docprint.document.close(); 
	   docprint.focus(); 
}
</script>